﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;

namespace PeisongWeb.admin
{
    public partial class customerList : adminPage
    {
        protected int companyId
        {
            get
            {
                int tmp;
                try { tmp = Convert.ToInt32(ViewState["companyId"]); }
                catch { tmp = 0; }
                return tmp;
            }
            set
            { ViewState["companyId"] = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (hasPermission("FC", 2))
                {
                    if (Request.QueryString["company"] != null)
                    {
                        if (Library.Number.IsIntPositive(Request.QueryString["company"]))
                        {
                            companyId = Convert.ToInt32(Request.QueryString["company"]);
                            btn_Add.OnClientClick = "window.location.href='customerEdit.aspx?company=" + companyId + "'";
                            lbl_Company.Text = logic.company.getById(companyId).Company;
                        }
                    }
                    rptBind();
                }
                else
                {
                    Library.Script.ClientMsgUrl("没有权限！", "index.aspx");
                    return;
                }
            }
        }
        void rptBind()
        {
            StringBuilder where = new StringBuilder();
            where.Append("companyId="+companyId);
            if (txt_username.Text.Trim() != "")
                where.AppendFormat("and username='{0}'",txt_username.Text.Trim());
            if (txt_realname.Text.Trim() != "")
                where.AppendFormat("and realname='{0}'", txt_realname.Text.Trim());
            if (txt_mobile.Text.Trim() != "")
                where.AppendFormat("and phone='{0}'", txt_mobile.Text.Trim());
            DataSet ds = logic.customer.select(where.ToString(), "customerId desc", AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex);
            AspNetPager1.RecordCount = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
            rpt_list.DataSource = ds.Tables[1];
            rpt_list.DataBind();
        }
        protected void rpt_list_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Del")
            {
                int customerId = Convert.ToInt32(e.CommandArgument);
                string infor;
                logic.customer.delete(customerId);
                rptBind();
                infor = "删除成功！";
                Library.Script.ClientMsg(infor);
            }
        }
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            rptBind();
        }
        protected void lbtn_Search_Click(object sender, EventArgs e)
        {
            rptBind();
        }
    }
}
